package com.vdong.interactive.course.rocketmq;

import com.alibaba.fastjson.JSON;
import com.vdong.interactive.course.common.enums.PeddleGoodsOpenEnum;
import com.vdong.interactive.course.entity.mo.GoodsPeddle;
import com.vdong.interactive.course.facade.PeddleGoodsFacadeService;
import com.vdong.interactive.course.repository.dao.GoodsPeddleMapper;
import com.vdong.kpay.basics.dubbo.api.frontend.AppUserCourseService;
import com.vdong.kpay.basics.dubbo.pojo.wechat.WechatOrder;
import com.vdong.rocketmq.TopicEnum;
import com.vdong.rocketmq.annotation.MQConsumeService;
import com.vdong.rocketmq.consumer.processor.AbstractMQMsgProcessor;
import com.vdong.rocketmq.consumer.processor.MQConsumeResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lid
 * @Date 2019/1/3 19:07
 */
@Slf4j
@MQConsumeService(topic = TopicEnum.Course, tags = {"*"})
public class CourseUpdateImpl extends AbstractMQMsgProcessor {

    @Autowired
    private PeddleGoodsFacadeService peddleGoodsFacadeService;

    @Autowired
    private GoodsPeddleMapper goodsPeddleMapper;

    @Override
    @Transactional
    protected MQConsumeResult consumeMessage(String tag, List<String> keys, MessageExt messageExt) {
        log.info("consumeMessage,入参为：[{}],[{}],[{}]", tag, keys == null ? "keys" : JSON.toJSONString(keys), messageExt);
        String body = new String(messageExt.getBody());
        Long courseId = new Long(body);
        log.info("courseId:"+courseId+"==========================");
        MQConsumeResult result = new MQConsumeResult();
        Date currentDateTime = new Date();
        if (tag.equals("noOn")) {
            try {

                GoodsPeddle goodsPeddle = new GoodsPeddle();
                goodsPeddle.setPlatformGoodsId(courseId);
                goodsPeddle.setDeleted(false);
                List<GoodsPeddle> goodsPeddleList = goodsPeddleMapper.select(goodsPeddle);

                if (goodsPeddleList == null || goodsPeddleList.size() < 1){
                    result.setSuccess(true);
                    return result;
                }

                GoodsPeddle goods = goodsPeddleList.get(0);
//                goods.setOpenState(PeddleGoodsOpenEnum.LOWER_SHELF.getCode());
//                goods.setModifyDate(currentDateTime);
//                goodsPeddleMapper.updateByPrimaryKeySelective(goods);
                Map<String,Object> map = new HashMap<>();
                map.put("peddleGoodsId",goods.getId());
                peddleGoodsFacadeService.delViolationPeddleGoods(JSON.toJSONString(map));
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("MQ监听捕获到的异常为:[{}]", e);
            }
        }
        result.setSuccess(true);
        return result;
    }

}
